package com.itheima.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.youdianda.common.Result;
import com.itheima.youdianda.domain.SysUser;
import com.itheima.youdianda.domain.YddArticle;
import com.itheima.youdianda.domain.YddCate;
import com.itheima.youdianda.domain.YddUser;
import com.itheima.youdianda.dto.SysCateDto;
import com.itheima.youdianda.service.SysConfigService;
import com.itheima.youdianda.service.SysUserService;
import com.itheima.youdianda.service.YddAdvimgService;
import com.itheima.youdianda.service.YddAdvposService;
import com.itheima.youdianda.service.YddArticleService;
import com.itheima.youdianda.service.YddCateService;
import com.itheima.youdianda.service.YddTagService;
import com.itheima.youdianda.service.YddUserService;
import com.itheima.youdianda.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 袁鹏
 * @date 2022-09-06-19:41
 */
@RestController
@RequestMapping("/index")
public class SysIndexController {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysConfigService sysConfigService;

    @Autowired
    private YddCateService yddCateService;

    @Autowired
    private YddAdvposService yddAdvposService;

    @Autowired
    private YddTagService yddTagService;

    @Autowired
    private YddArticleService yddArticleService;

    @Autowired
    private YddAdvimgService yddAdvimgService;

    @Autowired
    private YddUserService yddUserService;

    @PostMapping("/login")
    public Result login(@RequestBody SysUser loginUser){
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getLoginName, loginUser.getLoginName())
                .eq(SysUser::getPassword, DigestUtils.md5DigestAsHex(loginUser.getPassword().getBytes()));
        SysUser one = sysUserService.getOne(queryWrapper);
        if(one == null){
            return Result.error("密码不正确");
        }else if(one.getStatus() == 0){
            return Result.error("您的账户已被冻结");
        }else {
            Map<String, Object> map = new HashMap<>();
            map.put("id", one.getId());
            String token = JwtUtils.getToken(map);
            map = new HashMap<>();
            map.put("token", token);
            return Result.ok(map);
        }
    }

    @PostMapping("/profile")
    //@Cacheable(cacheNames = "profileCache", key = "#session.getAttribute(\"userId\")")
    public Result getUserInfo(HttpSession session){
        String id = (String) session.getAttribute("userId");
        Map<String, Object> map = sysUserService.getProfile(id);
        return Result.ok(map);
    }


    @GetMapping("/getConfig")
    //@Cacheable(cacheNames = "config")
    public Result getConfig(){
        Map<String, String> map = new HashMap<>();
        sysConfigService.list().forEach(i -> map.put(i.getAttrKey(), i.getAttrValue()));
        return Result.ok(map);
    }

    @GetMapping("/baseInfo")
    //@Cacheable(cacheNames = "baseInfo")
    public Result getBaseInfo(){
        Map<String, Object> map = new HashMap<>();
        List<SysCateDto> cateDtoList = yddCateService.getSysCateDto();
        cateDtoList.forEach(c -> c.setNum(yddArticleService.getCateNum(c.getId())));
        map.put("all_cate", cateDtoList);
        map.put("advpos_num", yddAdvposService.count());
        map.put("tag_num", yddTagService.count());
        map.put("admin_num", sysUserService.count());
        map.put("article_num", yddArticleService.count());
        map.put("cate_num", yddCateService.count());
        map.put("advimg_num", yddAdvimgService.count());
        LambdaQueryWrapper<YddUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .orderByDesc(YddUser::getId)
                .last("limit 7");
        map.put("new_user", yddUserService.list(queryWrapper));

        LambdaQueryWrapper<YddArticle> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.orderByDesc(YddArticle::getClick)
                        .last("limit 7");
        map.put("hot_article", yddArticleService.list(queryWrapper2));
        Map<String, Number> userMap = new HashMap<>();
        userMap.put("total", yddUserService.count());
        LambdaQueryWrapper<YddUser> queryWrapper3 = new LambdaQueryWrapper<>();
        queryWrapper3.eq(YddUser::getSex, 1);
        userMap.put("user_man", yddUserService.count(queryWrapper3));
        LambdaQueryWrapper<YddUser> queryWrapper4 = new LambdaQueryWrapper<>();
        queryWrapper4.eq(YddUser::getSex, 0);
        userMap.put("user_woman", yddUserService.count(queryWrapper4));
        map.put("user", userMap);
        return Result.ok(map);
    }


}
